<template>
  <!--录入入库信息-->
  <el-dialog title="新增入库信息" :visible.sync="goodPutDialogVisible" :append-to-body="appendToBody" width="60%" center
             @close="closeDialog" @open="openDialog">
    <el-form ref="goodPutForm" :model="goodPut" class="dialogForm" :rules="goodPutFromRules" v-loading="dialogLoading">
      <table class="mailTable">
        <tr class="firstTr">
          <td class="column firstTd">
            <span class="tip" v-if="groupIdRequired">*</span>组别
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="group_id" :rules="[
              { required: groupIdRequired, message: '请选择供应商', trigger: 'change' }
            ]">
              <el-select v-model="goodPut.group_id" clearable placeholder="请选择组别" style="width: 100%">
                <el-tooltip v-for="group in groupArr" :key="group.id" effect="dark" placement="right">
                  <div slot="content">
                    <div>组别所含销售单信息</div>
                    <div v-for="(item,index) in group.saleVOList" :key="group.id+''+index">
                      {{
                        item.contract_num + '——' + item.actual_execute_time_start + '~' + item.actual_execute_time_end
                      }}
                    </div>
                  </div>
                  <el-option :label="group.name" :value="group.id"></el-option>
                </el-tooltip>
              </el-select>
            </el-form-item>
            <span v-else>
              <el-tooltip placement="top">
                <div slot="content">
                  <div>组别所含销售单信息</div>
                  <div v-for="(item,index) in goodPut.billSaleVOList" :key="index">
                    {{ item.contract_num + '——' + item.actual_execute_time_start + '~' + item.actual_execute_time_end }}
                  </div>
                </div>
                <div>{{ goodPut.group_name }}</div>
              </el-tooltip>
            </span>
          </td>
          <td class="column">
            <span class="tip">*</span>入库人
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="user_id">
              <el-select v-model="goodPut.user_id" clearable placeholder="请选择入库人" style="width: 100%">
                <el-option v-for="user in userList" :key="user.id" :label="user.name" :value="user.id"></el-option>
              </el-select>
            </el-form-item>
            <span v-else>
              {{ goodPut.user_name }}
            </span>
          </td>
        </tr>
        <tr>
          <td class="column firstTd">
            <span class="tip">*</span>物品
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="goods_type_name">
              <el-select v-model="goodPut.goods_type_name" clearable filterable allow-create placeholder="请选择物品"
                         style="width: 100%"
              >
                <el-option v-for="(item,index) in goods_type_name_arr" :key="index" :value="item" :label="item"
                ></el-option>
              </el-select>
            </el-form-item>
            <span v-else>
              {{ goodPut.goods_name }}
            </span>
          </td>
          <td class="column">
            <span class="tip">*</span>数量
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="number">
              <el-input v-model.trim="goodPut.number" type="number" step="1" placeholder="请输入数量"></el-input>
            </el-form-item>
            <span v-else>
              {{ goodPut.number }}
            </span>
          </td>
        </tr>
        <tr class="firstTr">
          <td class="column firstTd">
            <span class="tip">*</span>单价
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="price">
              <el-input v-model.trim="goodPut.price" type="number" step="0.01" placeholder="请输入单价">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <span v-else>
              {{ goodPut.price + '元' }}
            </span>
          </td>
          <td class="column">
            <span class="tip">*</span>总价
          </td>
          <td class="column">
            <el-form-item v-if="!readonly" prop="total_price">
              <el-input :value="goodPut.total_price" readonly placeholder="系统自动计算">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <span v-else>
              {{ goodPut.total_price + '元' }}
            </span>
          </td>
        </tr>
        <tr class="firstTr">
          <td class="column firstTd">
            生产日期
          </td>
          <td class="column">
            <el-date-picker v-if="!readonly" v-model="goodPut.product_date" type="date" placeholder="请选择生产日期"
                            format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" style="width: 100%"
            >
            </el-date-picker>
            <span v-else>
              {{ goodPut.product_date }}
            </span>
          </td>
          <td class="column">
            到期日期
          </td>
          <td class="column">
            <el-date-picker v-if="!readonly" v-model="goodPut.due_date" type="date" placeholder="请选择到期日期"
                            format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" style="width: 100%"
            >
            </el-date-picker>
            <span v-else>
              {{ goodPut.due_date }}
            </span>
          </td>
        </tr>
      </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="goodPutDialogVisible = false">{{ readonly? '关 闭':'取 消' }}</el-button>
      <el-button v-if="!readonly" size="mini" type="primary" @click="saveInfo()">保&nbsp;存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { multiply } from '@/utils/amount'
import { list_str } from '@/api/goods/goods_type'
import { add, detail } from '@/api/goods/goods_put'

export default {
  name: 'GoodsPutDialog',
  props: {
    id: {
      type: Number,
      default: -1
    },
    readonly: {
      type: Boolean,
      default: false
    },
    groupArr: {
      type: Array,
      default() {
        return []
      }
    },
    userList: {
      type: Array,
      default() {
        return []
      }
    },
    appendToBody: {
      type: Boolean,
      default: false
    },
    groupIdRequired: {
      type: Boolean,
      default: true
    }
  },
  data() {
    var checkAccount = (rule, value, callback) => {
      if (value === '' || value === null) {
        callback(new Error('总价不能为空'))
      } else {
        const reg = /(^[1-9]{1}[0-9]*$)|(^[0-9]*\.[0-9]{2}$)/
        if (reg.test(value)) {
          callback()
        } else {
          callback(new Error('总价不是合法金额'))
        }
      }
    }
    return {
      dialogLoading: false,
      goods_type_name_arr: [],
      goodPutDialogVisible: false,
      goodPut: {
        id: null,
        group_id: null,
        goods_type_name: null,
        number: null,
        price: null,
        total_price: null,
        product_date: null,
        due_date: null,
        user_id: null
      },
      goodPutFromRules: {
        // group_id: [
        //   { required: true, message: '请选择供应商', trigger: 'change' }
        // ],
        goods_type_name: [
          { required: true, message: '请选择物品', trigger: 'change' }
        ],
        user_id: [
          { required: true, message: '请选择入库人', trigger: 'change' }
        ],
        number: [
          { required: true, message: '请输入数量', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入单价', trigger: 'blur' }
        ],
        total_price: [
          { validator: checkAccount, trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'goodPut.price': function(val) {
      if (val && !isNaN(val) && this.goodPut.number && !isNaN(this.goodPut.number)) {
        this.goodPut.total_price = multiply(this.goodPut.price, this.goodPut.number)
      } else {
        this.goodPut.total_price = null
      }
    },
    'goodPut.number': function(val) {
      if (val && !isNaN(val) && this.goodPut.price && !isNaN(this.goodPut.price)) {
        this.goodPut.total_price = multiply(this.goodPut.price, this.goodPut.number)
      } else {
        this.goodPut.total_price = null
      }
    }
  },
  created() {
    this.initGoodsTypeNameList()
  },
  methods: {
    closeDialog() {
      this.goodPut = {
        group_id: null,
        goods_type_name: null,
        number: null,
        price: null,
        total_price: null,
        product_date: null,
        due_date: null,
        user_id: null
      }
      this.$nextTick(() => {
        this.$refs.goodPutForm.clearValidate()
      })
    },
    initGoodsTypeNameList() {
      list_str().then(res => {
        this.goods_type_name_arr = res.data
      }).catch(() => {

      })
    },
    saveInfo() {
      this.$refs.goodPutForm.validate(flag => {
        if (flag) {
          const loading = this.$loading({
            lock: true,
            text: '保存中',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          add(this.goodPut).then(res => {
            loading.close()
            this.$message.success('操作成功')
            this.goodPutDialogVisible = false
            this.initGoodsTypeNameList()
            this.$emit('reload')
          }).catch(() => {
            loading.close()
          })
        }
      })
    },
    show() {
      this.goodPutDialogVisible = true
    },
    openDialog() {
      if (this.readonly && this.id !== -1) {
        this.dialogLoading = true
        detail(this.id).then(res => {
          this.goodPut = res.data
          this.groupIdRequired = res.data.group_id ? true : false
          this.dialogLoading = false
        }).catch(() => {

        })
      }
    }
  }
}
</script>

<style scoped>
.mailTable {
  width: 90%;
  font-size: 14px;
  margin: 0 auto 10px auto;
  border-collapse: collapse;
}

.mailTable tr td {
  width: 200px;
  height: 60px;
  line-height: 35px;
  box-sizing: border-box;
  padding: 0 10px;
  border-bottom: 1px solid #E6EAEE;
  border-right: 1px solid #E6EAEE;
}

.firstTr td {
  border-top: 1px solid #E6EAEE;
}

.firstTd {
  border-left: 1px solid #E6EAEE;
}

.mailTable tr td.column {
  text-align: center;
}

.tip {
  color: red;
}

/deep/ .dialogForm .el-form-item {
  margin-bottom: 0;
}
</style>
